﻿Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
'Imports System.Web.UI.WebControls.WebParts
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
Imports Microsoft.SharePoint.WebPartPages


<ToolboxItemAttribute(False)> _
Public Class SubWebs
    Inherits WebPart

    Protected WithEvents SubWebList As New DropDownList

    'these constants holds the default values for the webpart properties
    Private Const _defaultText As String = ""
    Private Const _defaultImage As Boolean = True
    Private Const _defaultBool As Boolean = True
    Private Const _defaultDesc As Boolean = False
    Private Const _defaultdropdown As Boolean = False
    Private Const _defaultIcon As Boolean = False
    Private Const _defaultsort As Boolean = False

    Public Enum EnumSort
        Ascending = 0
        Descending = 1

    End Enum

    Dim _text As String = _defaultText
    Dim _bolImage As Boolean = _defaultImage
    Dim _bool As Boolean = _defaultBool
    Dim _Desc As Boolean = _defaultDesc
    Dim _dropdown As Boolean = _defaultdropdown
    Dim _Icon As Boolean = _defaultIcon
    Public Shared _Sort As Boolean = _defaultsort
    Public Shared _sortOrder As Integer = 0






    'Webpart property to show small square image in front of listing 
    <WebParts.Personalizable(WebParts.PersonalizationScope.[Shared]), _
   WebParts.WebBrowsable(True), DefaultValue(_defaultImage), WebParts.WebDisplayName("Show bullet in list"), Category("Sharepoint247.com"), WebParts.WebDescription("Show bullet in list")> _
    Property ShowImage() As Boolean
        Get
            Return _bolImage
        End Get
        Set(ByVal Value As Boolean)
            _bolImage = Value
        End Set
    End Property

    'webpart property, true will open link in new browser window
    <WebParts.Personalizable(WebParts.PersonalizationScope.[Shared]), _
    WebParts.WebBrowsable(True), DefaultValue(_defaultBool), WebParts.WebDisplayName("Open link in new Window"), Category("Sharepoint247.com"), WebParts.WebDescription("Open link in new Window")> _
    Property NewWindow() As Boolean
        Get
            Return _bool
        End Get
        Set(ByVal Value As Boolean)
            _bool = Value
        End Set
    End Property

    'Webpart property, set to true to show site description togheter with the sitename
    <WebParts.Personalizable(WebParts.PersonalizationScope.[Shared]), _
    WebParts.WebBrowsable(True), DefaultValue(_defaultDesc), WebParts.WebDisplayName("Show site description"), Category("Sharepoint247.com"), WebParts.WebDescription("Show site description")> _
    Property ShowDesc() As Boolean
        Get
            Return _Desc
        End Get
        Set(ByVal Value As Boolean)
            _Desc = Value
        End Set
    End Property

    'webpart property, set to true to show an icon representing the type of site, will show differnet builtin icons for team site, meeting workspace etc
    <WebParts.Personalizable(WebParts.PersonalizationScope.[Shared]), _
    WebParts.WebBrowsable(True), DefaultValue(_defaultIcon), WebParts.WebDisplayName("Show icon"), Category("Sharepoint247.com"), WebParts.WebDescription("Show icon for type of site")> _
    Property ShowIcon() As Boolean
        Get
            Return _Icon
        End Get
        Set(ByVal Value As Boolean)
            _Icon = Value
        End Set
    End Property

    'webpart property, if set to true the subsites will be dispalyed in a dropdown list instead of a list, use this to save space if you have a lot of subsites
    <WebParts.Personalizable(WebParts.PersonalizationScope.[Shared]), _
    WebParts.WebBrowsable(True), DefaultValue(_defaultdropdown), WebParts.WebDisplayName("Show as Dropdown list"), Category("Sharepoint247.com"), WebParts.WebDescription("Show sites in dropdown list")> _
    Property ShowDropDown() As Boolean
        Get
            Return _dropdown
        End Get
        Set(ByVal Value As Boolean)
            _dropdown = Value

        End Set
    End Property

    'webpart property, set to true to sort websites by createddate.
    <WebParts.Personalizable(WebParts.PersonalizationScope.[Shared]), _
    WebParts.WebBrowsable(True), DefaultValue(_defaultsort), WebParts.WebDisplayName("Sort by Date"), Category("Sharepoint247.com"), WebParts.WebDescription("Sort the sites by created date")> _
    Property SortDate() As Boolean
        Get
            Return _Sort
        End Get
        Set(ByVal Value As Boolean)
            _Sort = Value
        End Set
    End Property

    ' Creates a custom property that is an enum. This property will be displayed as a drop-down list in the property pane. Used for sorting the list

    <WebParts.Personalizable(WebParts.PersonalizationScope.[Shared]), _
    WebParts.WebBrowsable(True), DefaultValue(EnumSort.Ascending), WebParts.WebDisplayName("Sort order"), Category("Sharepoint247.com"), WebParts.WebDescription("Select to sort ascending or decending")> _
    Property SortOrder() As EnumSort

        Get
            Select Case _sortOrder
                Case 0
                    Return EnumSort.Ascending
                Case 1
                    Return EnumSort.Descending
            End Select

        End Get
        Set(value As EnumSort)
            Select Case value
                Case EnumSort.Ascending
                    _sortOrder = EnumSort.Ascending
                Case EnumSort.Descending
                    _sortOrder = EnumSort.Descending

            End Select

        End Set
    End Property




		
	

    Protected Overrides Sub CreateChildcontrols()

        'get current web
        Dim myWeb As SPWeb = SPControl.GetContextWeb(Context)

        'if webpart property is set to show as dropdown list use this portion of code
        If ShowDropDown = True Then
            SubWebList.AutoPostBack = True



            'Add dummy item at top of dropdown list, this adds the text "Select Site or Workspace" as the top item in the dropdown box.
            SubWebList.Items.Add(New ListItem("Select Site or Workspace", "NOSITE"))


            Try
                'Get SPWeb object representing the current site


                'Get collection of subsites the user has permissions to see
                Dim subSites As SPSortedWebCollection = New SPSortedWebCollection(myWeb.GetSubwebsForCurrentUser, "Created")


                'loop through the subsites collection
                'Dim intY As Integer
                For Each child As SPWeb In subSites


                    'add subsite title and url to the dropdown list
                    SubWebList.Items.Add(New ListItem(child.Title, child.Url))

                Next

                'Center the dropdown list in the webpart.
                'This should probably be removed and styled with CSS instead.
                Dim wssLit As New Literal
                wssLit.Text = "	<div style='text-align:center'>"
                'add the div tag to center the dropdown list to the Controls collection of the webpart
                Controls.Add(wssLit)
                'add the dropdown list to the webparts controls collection
                Controls.Add(SubWebList)
                'add antoher Literal control to the controls collection, this is to close the Div tag
                Dim wssCloseLit As New Literal
                wssCloseLit.Text = "</Div>"
                Controls.Add(wssCloseLit)

                'catch any errors
            Catch
                Dim lblError As New Label
                lblError.Text = "An unknow error occured."
                Controls.Add(lblError)
            End Try


            'This Else clause is used if Show subsites as list is selected E.g ShowDropDown = False
        Else
            'create a new table
            Dim table1 As New Table

            table1.CellPadding = 2

            Try

                'Get collection of subsites the user has permissions to see
                Dim subSites As SPSortedWebCollection = New SPSortedWebCollection(myWeb.GetSubwebsForCurrentUser, "Created")


                'Loop through the subsites collection and add a table row to table1 for each subsite
                For Each child As SPWeb In subSites
                    Dim kisRow As New TableRow
                    Dim strLink As String

                    Dim kisCell1 As New TableCell
                    'If Open link in new window is set add the target=_new attribute to the <a href tag
                    If _bool Then
                        strLink = "<a href='" & child.Url & "' target=_nsp>" & child.Title & "</a>"
                        'Else just add the link
                    Else
                        strLink = "<a href='" & child.Url & "'>" & child.Title & "</a>"
                    End If
                    'Check if we should show icon
                    If ShowIcon Then
                        'If the site is a Meeting Workspace add the Meeting workspace icon
                        If child.WebTemplate.ToUpper = "MPS" Then
                            strLink = "<img src=/_layouts/images/mtgicon.gif alt='Meeting Workspace'> " & strLink
                            'If the subsite is a Document Workspace add the document workspace icon
                        ElseIf child.WebTemplate.ToUpper = "STS" AndAlso child.Configuration = 2 Then
                            strLink = "<img src=/_layouts/images/docicon.gif ALT='Document Workspace'> " & strLink
                            'for all other sites add the standard Team Site icon
                        Else
                            strLink = "<img src=/_layouts/images/stsicon.gif ALT='Team Site'> " & strLink
                        End If

                    End If

                    'check if we should show a image bullet
                    If _bolImage Then
                        Dim kisCell As New TableCell
                        'this is a standard Sharepoint gif
                        kisCell.Text = "<img src=/_layouts/images/square.gif>"
                        kisRow.Cells.Add(kisCell)
                    End If

                    'add the link text to the table cell
                    kisCell1.Text = strLink
                    kisRow.Cells.Add(kisCell1)
                    table1.Rows.Add(kisRow)
                    'if Show Description is set to true we will add a cell to show the Site Descrition as well. this description is the description you put on the site when you created the subsites
                    If _Desc Then
                        Dim nspRow As New TableRow
                        Dim nspCell As New TableCell
                        Dim nspCell1 As New TableCell
                        nspCell1.Text = ""

                        nspCell.Text = child.Description
                        nspRow.Cells.Add(nspCell1)
                        nspRow.Cells.Add(nspCell)
                        table1.Rows.Add(nspRow)
                    End If
                Next

                'that's it, add the table to the webparts control collection and your done
                Controls.Add(table1)
                'catch any errors
            Catch
                Dim lblError As New Label
                lblError.Text = "Error getting subsites"
                Controls.Add(lblError)
            End Try
        End If
        'Execute the CreateChildControls collection to render the webpart
        MyBase.CreateChildControls()


    End Sub
    Public Overrides Function GetToolParts() As ToolPart()
        Dim toolParts(3) As ToolPart
        Dim wptp As WebPartToolPart = New WebPartToolPart
        Dim custom As CustomPropertyToolPart = New CustomPropertyToolPart
        Dim cust247 As CustomToolPart = New CustomToolPart
        cust247.Title = "Visit our website"
        'Dim custom As CustomToolPart = New CustomToolPart
        'custom.Title = "Logo"
        toolparts(0) = wptp
        toolParts(1) = custom
        toolParts(2) = cust247

        Return toolparts

    End Function

    'Public Function GetCustomToolParts() As Microsoft.SharePoint.WebPartPages.ToolPart()
    '    Dim toolParts(1) As WebPartPages.ToolPart
    '    Dim MyTp As New CustomToolPart
    '    MyTp.Title = "Custom Properties"

    '    toolParts(0) = MyTp
    '    Return toolParts
    'End Function

    'this is the SelectedIndexChanged method of the Dropdown list
    'It is executed if you selct to show the subsites list as a dropdown box. It is executed if you select an item in the drpdown list
    'It uses the Response.Redirect method of the page to naviagte to the URL of the selcted subsite.
    'NOTE: If you choose to display the list as a dropdown list the Subsites will not be opened in a new browser window, it will allways open in the same browser window.
    Private Sub SubWebList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubWebList.SelectedIndexChanged

        If Not SubWebList.SelectedValue = "NOSITE" Then
            Me.Page.Response.Redirect(SubWebList.SelectedValue)
        End If

    End Sub


End Class
